package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.NewsContent;
import cn.wolfcode.trip.base.domain.NewsPage;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.mapper.NewsContentMapper;
import cn.wolfcode.trip.base.mapper.NewsPageMapper;
import cn.wolfcode.trip.base.query.NewPagesQueryObject;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelCommentQueryObject;
import cn.wolfcode.trip.base.service.INewsPageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class NewsPageServiceImpl implements INewsPageService {

    @Autowired
    private NewsPageMapper newsPageMapper;

    @Autowired
    private NewsContentMapper newsContentMapper;
    @Override
    public void update(NewsPage newsPage) {
        //修改发布时间
        if(newsPage.getState()==NewsPage.STATE_RELEASE) {
            newsPage.setReleaseTime(new Date());
        }
        newsPageMapper.updateByPrimaryKey(newsPage);
        //更新文本内容
        NewsContent newsContent=new NewsContent();
        newsContent.setId(newsPage.getId());
        newsContent.setContent(newsPage.getNewsContent().getContent());
        newsContentMapper.updateByPrimaryKey(newsContent);

    }

    @Override
    public void save(NewsPage newsPage) {
      
        Date date=new Date();
        //创建时间
        newsPage.setCreateTime(date);
        //发布时间,先判断
        if(newsPage.getState()==NewsPage.STATE_RELEASE) {
            newsPage.setReleaseTime(date);
        }
        newsPageMapper.insert(newsPage);
        //保存文本内容
        NewsContent newsContent=new NewsContent();
        newsContent.setId(newsPage.getId());
        newsContent.setContent(newsPage.getNewsContent().getContent());
        newsContentMapper.insert(newsContent);
    }


    @Override
    public List<NewsPage> listAll() {
        return  newsPageMapper.selectAll();
    }

    @Override
    public PageInfo<NewsPage> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<NewsPage> list=newsPageMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public NewsContent getContentById(Long id) {
        return newsContentMapper.selectContentById(id);
    }


    @Override
    public NewsPage get(Long id) {
        return newsPageMapper.selectByPrimaryKey(id);
    }

    /**
     * app端的分页及查询
     * @param qo
     * @return
     */
    @Override
    public PageInfo queryForApp(TravelCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<NewsPage> list=newsPageMapper.selectForAppList(qo);
        return new PageInfo(list);
    }

    @Override
    public NewsPage selectByPrimaryKey(Long id) {
        return newsPageMapper.selectByPrimaryKey(id);
    }

    @Override
    public NewsPage selectBiggerNews() {
        return newsPageMapper.selectBiggerNews();
    }

    @Override
    public PageInfo getNewsPageCounts(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<NewsPage> list=newsPageMapper.selectList(qo);
        return new PageInfo<>(list);
    }
}
